﻿using LightingPlatform.TCP;
using LightingPlatform.Tools;
using LightingPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform.View.UserManager
{
    public partial class UserManagerWindow : ChildWindow
    {
        private ObservableCollection<UserVM> _items = new ObservableCollection<UserVM>();

        public UserManagerWindow()
        {
            InitializeComponent();

            AddUserBtn.Click += AddUserBtn_Click;
            dataGrid1.ItemsSource = _items;
            Loading();
        }


        private void AddUserBtn_Click(object sender, RoutedEventArgs e)
        {
            UserWindow window = new UserWindow();
            window.Closed += (sender1, e1) =>
            {
                if (window.DialogResult.HasValue && window.DialogResult.Value)
                {
                    UserVM userVM = new UserVM()
                    {
                        ID = window.User.Id,
                        Name = window.User.Name,
                        Password = window.User.Password,
                        RoleType = window.User.RoleType
                    };
                    _items.Add(userVM);
                }

            };
            window.Show();
        }

        private async void Loading()
        {
            WebServiceProxy proxy = new WebServiceProxy();
            UserArray userArray = await proxy.GetUsers();
            foreach (User user in userArray.UserList)
            {
                _items.Add(CreateUser(user));
            }
        }

        private UserVM CreateUser(User user)
        {
            return new UserVM()
            {
                ID = user.Id,
                Name = user.Name,
                Password = user.Password,
                RoleType = user.RoleType
            };
        }

        private void Modify_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            UserVM userVM = button.DataContext as UserVM;
            if (userVM != null)
            {
                UserWindow window = new UserWindow(userVM, true);
                window.Show();
            }
        }

        private async void Delete_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            UserVM userVM = button.DataContext as UserVM;
            if (userVM != null)
            {
                if (MessageHelper.Confirm("是否删除用户？"))
                {
                    WebServiceProxy proxy = new WebServiceProxy();
                    bool result = await proxy.DeleteUser(userVM.ID);
                    if (result)
                    {
                        _items.Remove(userVM);
                    }
                    else
                    {
                        MessageHelper.ShowError("删除用户失败。");
                    }
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }

    public class RoleTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //1普通用户、2经理、3数据导入者、4系统管理员
            uint intValue = uint.Parse(value.ToString());
            switch (intValue)
            {
                case 1:
                    return "普通用户";
                case 2:
                    return "高级用户";
                case 3:
                    return "数据库管理员";
                case 4:
                    return "系统管理员";
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

